package auto.setting;


import auto.common.Login;
import auto.common.Registartion;
import auto.device.Phione;

import io.appium.java_client.AppiumBy;
import io.appium.java_client.AppiumDriver;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.TimeoutException;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//这个类里面是针对账户列表的，并不进入详情页面
public class AccountList {
//feature1:账户列表账号数量检测（账号登录上限检测）  检测上限的时候也会执行登录
    public String accountUpperLimitDetection() throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;
        System.out.println("开始检查当前登录账号情况");
        Thread.sleep(1000);

        //用于计数当前登录的账号有多少
        int i=1;

        //首先检查设置按钮是不是存在
        try {
            //查看设置按钮是不是存在,存在的话就点进去
            driver.findElement(AppiumBy.accessibilityId("设置")).click();

            //运行到这里设置就是存在，然后查看已经登录了几个账号,并且此时最少已经登录了一个账号，开始从第二个账号检查
            i=2;
            while(i<6){
                String userPath = "**/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                        "/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                        "/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell["+i+"]";
                WebElement user=driver.findElement(AppiumBy.iOSClassChain(userPath));

                //添加账号这一行会当做第五个用户，这里做一个检测,只有添加账号会有 name属性
                if(user.getAttribute("name")==null){
                    System.out.println("第"+i+"个账号已登录");
                }
                else{
                    break;
                }
                i++;
            }

            System.out.println("当前登录账号："+(i-1));

            // 如果进入到这个判断证明已经登录了5个账号，此时点击添加账号再进入登录页面就是错误的
            if(i==6){
                String result=accountDetection(wait,driver);
                if(!result.equals("正常")){
                    System.out.println("上限检测异常");
                    driver.quit();
                    return result;
                }
                driver.quit();
                return "当前登录最大账号数量为5，登录上限检测正常";
            }

        } catch (NoSuchElementException e) {//当前页面没有设置按钮
            if(i==1){
                System.out.println("当前设备没有登录任何账号");
            }
        }


        //判断i的值为多少，开始登录对应的账号
        Long phone=10000000001L;
        if(i==1){//如果i为1就是1个账号都没登录
            while(i<6){
                //如果是从添加账号进行的登录，就会执行段代码。如果设备没有登录任何账号，就不会执行
                try {
                    Thread.sleep(1000);
                    //点击设置
                    driver.findElement(AppiumBy.accessibilityId("设置")).click();

                    //点击添加账号
                    WebElement account = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("addAccount")));
                    account.click();
                } catch (NoSuchElementException e) {

                }

                Login.login(wait,driver,phone.toString(),"123456");

                //登录后检测账号信息有没有出现
                if(activateAccountDataLoadingInspect(wait,driver)){
                    System.out.println("登录后账号信息没有记载");
                    driver.quit();
                    return "登录后账号信息没有加载";
                }

                phone++;
                i++;
            }

            if(i==6){
                //点击设置
                driver.findElement(AppiumBy.accessibilityId("设置")).click();

                //检测的这个函数里面点击添加账号
                String result=accountDetection(wait,driver);
                if(!result.equals("正常")){
                    System.out.println("上限检测异常");
                    driver.quit();
                    return result;
                }
            }
        }
        else{//这个地方就是另外的判断了，已经登录了账号但是没有登录满，继续登录
            //这个地方i为几就是已经登录了 i-1 个账号
            phone+=(i-1);

            while(i<6){
                //如果是从添加账号进行的登录，就会执行段代码。如果设备没有登录任何账号，就不会执行
                try {
                    Thread.sleep(1000);
                    //点击设置
                    driver.findElement(AppiumBy.accessibilityId("设置")).click();

                    //点击添加账号
                    WebElement account = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("addAccount")));
                    account.click();
                } catch (NoSuchElementException e) {

                }

                Login.login(wait,driver,phone.toString(),"123456");

                //登录后检测账号信息有没有出现
                if(activateAccountDataLoadingInspect(wait,driver)){
                    driver.quit();
                    return "登录后账号信息没有加载";
                }

                phone++;
                i++;
            }

            if(i==6){
                //点击设置
                driver.findElement(AppiumBy.accessibilityId("设置")).click();

                //检测的这个函数里面点击添加账号
                String result=accountDetection(wait,driver);
                if(!result.equals("正常")){
                    System.out.println("上限检测异常");
                    driver.quit();
                    return result;
                }
            }
        }


        System.out.println("当前最大登录账号数量为：5，登录上限检测正常");
        driver.quit();
        return "当前最大登录账号数量为：5，登录上限检测正常";
    }



//feature2:账户列表未读信息检测
    public String accountMessageDetection(){
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

       //点击设置按钮进入到设置页面
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("设置"))).click();
        }catch(TimeoutException e){
            System.out.println("请把软件处于软件主页面");
            driver.quit();
            return "请把软件处于软件主页面";
        }

       //开始查找当前账户列表里面存不存在未读消息的账户,最少存在两个账户才有可能存在未读消息
        int i=2;
        while(i<6){
            String userPath = "**/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                    "/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                    "/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell["+i+"]";
            WebElement user=driver.findElement(AppiumBy.iOSClassChain(userPath));

            //添加账号这一行会当做第五个用户，这里做一个检测,只有添加账号会有 name属性
            if(user.getAttribute("name")==null){//第一个判断只是判断是不是那个"添加账号"
                String message=userPath+"/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeStaticText";
                List<WebElement> messageElement=driver.findElements(AppiumBy.iOSClassChain(message));
                if(messageElement.size()==2){//判断有没有未读消息的
                    System.out.println("第"+i+"个账户存在未读消息");
                    //存在未读消息就点击,切换账号之后就在聊天页面了
                    user.click();

                    if(!driver.findElement(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"聊天\"`]")).getAttribute("value").equals("1")){//检测聊天页面存在不存在未读消息
                        System.out.println("聊天页面存在未读消息");
                        break;
                    }
                    else{
                        System.out.println("当前页面不存在未读消息");
                        driver.quit();
                        return "切换账号后不存在未读消息";
                    }
                }
                else{
                    System.out.println("第"+i+"个账号已登录,当前账号不存在未读消息");
                }
            }
            else{//检测到添加账号这一行还没遇见未读消息
                if(i==5){
                    System.out.println("当前登录所有账号不存在未读消息");
                    driver.quit();
                    return "当前登录账号不存在未读消息";
                }
                break;
            }
            i++;
        }


        System.out.println("未读消息显示正确");
        driver.quit();
        return "未读消息显示正确";
    }


//feature3:账户列表账户切换检测
    public String accountSwitching() throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        //点击设置按钮进入到设置页面
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("设置"))).click();
        }catch(TimeoutException e){
            System.out.println("请把软件处于软件主页面");
            driver.quit();
            return "请把软件处于软件主页面";
        }

        //检测登录了几个账号
        String firstUserPath = "**/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[1]";
        String secondUserPath = "**/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[2]";


        WebElement firstUserElement=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain(firstUserPath)));
        List<WebElement> firstNickNameAndUSerName=firstUserElement.findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText"));
        //最开始的第一个账号的信息
        String firstUSerInformation=firstNickNameAndUSerName.get(0).getAttribute("value");

        //对第二个元素定位,但是第二个元素可能是"添加账户"
        WebElement secongUserElement=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain(secondUserPath)));

        //如果为空直接执行切换的逻辑,如果消息不为空则执行添加账号的逻辑
        if(secongUserElement.getAttribute("name")==null){
            secongUserElement.click();
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("设置"))).click();

            //重新定位第一个元素,如果第一个元素不变则切换失败
            WebElement firstUserElementNew=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain(firstUserPath)));
            List<WebElement> firstNickNameAndUSerNameNew=firstUserElementNew.findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText"));
            //最开始的第一个账号的信息
            String firstUSerInformationNew=firstNickNameAndUSerNameNew.get(0).getAttribute("value");

            if(firstUSerInformationNew.equals(firstUSerInformation)){
                System.out.println("切换后消息不变,切换账号失败");
                driver.quit();
                return "切换后消息不变,切换账号失败";
            }

        }else{//如果不为空执行的就是添加账号的逻辑
            secongUserElement.click();
            System.out.println("缺少账户,登录账号:10000000002");
            Login.login(wait,driver,"10000000002","123456");

            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("设置"))).click();

            //登录之后第二个元素就是用户了,而且是之前的用户,点击进行切换
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain(secondUserPath))).click();
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("设置"))).click();


            //重新定位第一个用户,如果和之前存的信息一样就切换成功
            WebElement firstUserElementNew=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain(firstUserPath)));
            List<WebElement> firstNickNameAndUSerNameNew=firstUserElementNew.findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText"));
            String firstUSerInformationNew=firstNickNameAndUSerNameNew.get(0).getAttribute("value");


            //如果切换后不一样就是错误的
            if(!firstUSerInformationNew.equals(firstUSerInformation)){
                System.out.println("无法切换账户");
                driver.quit();
                return "无法切换账户";
            }
        }

        System.out.println("账户列表切换账户正常");
        driver.quit();
        return "账户列表切换账户正常";
    }


//feature4:在设置页面点击添加账号进行登录
    public String addAccountButtonLogIn(String accountNumber,String passWord) throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;
        Thread.sleep(1000);
        try{
            //检查主页面的设置按钮是否存在
            if(driver.findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"设置\"`]")).size()==0){
                System.out.println("请前往主页面再开始程序");
                driver.quit();
                return "请前往主页面再开始程序";
            }

            driver.findElement(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"设置\"`]")).click();
            //检查是否有第五个账号
            String userPath = "**/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                    "/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                    "/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[5]";
            WebElement user=driver.findElement(AppiumBy.iOSClassChain(userPath));
            if(user.getAttribute("name")==null){
                System.out.println("第5个账号已登录，请退出一个账号");
                driver.quit();
                return "第五个账号已登录，请退出一个账号";
            }

            //点击添加账户
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("添加账户"))).click();

            //开始登录
            String result=Login.login(wait,driver,accountNumber,passWord);
            if(result.equals("正常")){
                System.out.println("添加账号按钮正常");
                driver.quit();
                return "添加账号按钮正常";
            }
            else{
                return result;
            }

        }catch(TimeoutException | NoSuchElementException e1){
            System.out.println("设置页面添加账号按钮功能异常，请检查");
            driver.quit();
            return "设置页面添加账号按钮按钮有功能异常，请检查";
        }
    }



//feature5:点击添加账号按钮进行注册
public String addAccountButtonRegistartion(String accountNumber,String passWord) throws InterruptedException {
    Phione phione = new Phione();
    AppiumDriver driver = phione.driver;
    WebDriverWait wait = phione.wait;
    Thread.sleep(1000);
    try{
        //检查主页面的设置按钮是否存在
        if(driver.findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"设置\"`]")).size()==0){
            System.out.println("请前往主页面再开始程序");
            driver.quit();
            return "请前往主页面再开始程序";
        }

        driver.findElement(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"设置\"`]")).click();
        //检查是否有第五个账号
        String userPath = "**/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[5]";
        WebElement user=driver.findElement(AppiumBy.iOSClassChain(userPath));
        if(user.getAttribute("name")==null){
            System.out.println("最多登录五个账号，请退出一个账号");
            driver.quit();
            return "最多登录五个账号，请退出一个账号";
        }

        //点击添加账户
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("添加账户"))).click();

        //开始注册
        String result= Registartion.registartion(wait,driver,accountNumber,passWord);
        if(result.equals("正常")){
            System.out.println("添加账号按钮正常");
            driver.quit();
            return "添加账号按钮正常";
        }
        else{
            return result;
        }

    }catch(TimeoutException | NoSuchElementException e1){
        System.out.println("设置页面添加账号按钮功能异常，请检查");
        driver.quit();
        return "设置页面添加账号按钮按钮有功能异常，请检查";
    }
}






    //------------------下方的私有方法供上方的公有方法使用-------------------------

    //feature：检查当前激活账号数据是否加载，这个方法执行完就已经在设置页面了，返回true就是没有加载，返回false就是数据加载了
    private boolean activateAccountDataLoadingInspect(WebDriverWait wait, AppiumDriver driver) throws InterruptedException {
        //首先点击设置页面
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"设置\"`]"))).click();


        //用户列表中第一个用户的路径
        String firstUser="**/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[1]";


        //下滑三次第一个账户数据没有出现就是加载失败了
        int count=0;
        //如果第一个账号检测不出来就向下滑，直到出现为止
        do{
            Map<String, Object> argsDown = new HashMap<>();
            argsDown.put("direction", "down");
            driver.executeScript("mobile: swipe", argsDown);
            count++;
            if(count==3){
                break;
            }
        }while(driver.findElements(AppiumBy.iOSClassChain(firstUser)).size()==0);

        if(count>=3){
            return  true;
        }

        //保存下来当前激活账号的用户名和昵称元素
        List<WebElement> nickNameAndUSerName=driver.findElement(AppiumBy.iOSClassChain(firstUser)).
                findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText"));
        if(nickNameAndUSerName.size()==0){
            return true;//数据真的不存在
        }
        else{
            return false;//数据已经加载了
        }
    }




    //feature:用于检测账号的上限（出现图标提示，以及没有进入登录账号的页面）
    public static String accountDetection(WebDriverWait wait, AppiumDriver driver){

        //点击添加账号的按钮并判断是否出现
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("addAccount"))).click();

        //查看"进入"的那个按钮是不是出现了,没有进入这个页面就是正常的
        try{
            driver.findElement(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name == \"进入\"`]"));
            System.out.println("账号已达上限仍然能进入登录页面");
            driver.quit();
            return "账号已达上限仍然能进入登录页面";
        }catch(NoSuchElementException e){
            System.out.println("上限检测正常");
        }

        return "正常";
    }




}
